﻿

namespace LxCFD
{
    public class StepsCenter : IReset
    {
        public StepsAndStop SteadyStep = new StepsAndStop();
        public StepsAndStop TransientStep = new StepsAndStop();
        public int TotalStepCount;
        public void Reset(ResetTypes clearType)
        {
            switch (clearType)
            {
                case ResetTypes.ClearToLoadSimulation:
                case ResetTypes.ClearToLoadMesh:
                case ResetTypes.ClearFields:
                    SteadyStep.StepsCount = 0;
                    TransientStep.StepsCount = 0;
                    TotalStepCount = 0;
                    break;
                case ResetTypes.UpdateSolveCenter:
                case ResetTypes.None:
                default:
                    break;
            }
        }
        public void MoveNextStep_Steady()
        {
            SteadyStep.ResetStopRule();
            SteadyStep.StepsCount++;
            TotalStepCount++;
        }
        public void MoveNextStep_Transient()
        {
            TransientStep.ResetStopRule();
            TransientStep.StepsCount++;
            SteadyStep.StepsCount = 0;
        }
        public class StepsAndStop
        {
            public int StepsCount;
            public int MaxSteps = 2;

            protected bool isStop_Or = false;
            protected bool isStop_And = true;
            protected bool isStop_And_isUsed = false;
            public void ResetStopRule()
            {
                isStop_Or = false;
                isStop_And = true;
                isStop_And_isUsed = false;
            }
            public void AddStopRule(bool isForce, bool isStop)
            {
                if (isForce)
                {
                    isStop_Or = isStop_Or || isStop;
                }
                else
                {
                    isStop_And_isUsed = true;
                    isStop_And = isStop_And && isStop;
                }
            }
            public bool IsContinue()
            {
                AddStopRule(true, StepsCount >= MaxSteps);
                if (isStop_And_isUsed)
                {
                    return !(isStop_Or || isStop_And);
                }
                else
                {
                    return !isStop_Or;
                }
            }
        }
    }

}
